//
//  URAnnouceViewContoller.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/10/20.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

struct AonnouceInfo {
    var message:String!
    var url:String?
    var imageUrl:String?
    
    init(message:String,url:String?) {
        self.message = message
        self.url = url
    }
}

class URAnnouceViewContoller: NSViewController {
    
    var info:AonnouceInfo!
    
    override func loadView() {
        let view = NSView.init(frame: NSRect.init(origin: CGPoint.zero, size: CGSize.init(width: 420, height: 300)))
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(255).cgColor
        self.view = view
    }
    
    convenience init(info:AonnouceInfo) {
        self.init()
        self.info = info
        self.setupUI()
        self.loadData()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.refreshrtingWindow()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func loadData() {
        self.mssage.stringValue = self.info.message
        //self.openButtom.isHidden = self.info.url == nil
    }
    
    
    func refreshrtingWindow() {
        self.view.window?.standardWindowButton(NSWindow.ButtonType.closeButton)?.isHidden = true
        self.view.window?.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)?.isHidden = true
        self.view.window?.standardWindowButton(NSWindow.ButtonType.zoomButton)?.isHidden = true
        self.view.window?.styleMask = [.titled,.fullSizeContentView]
        self.view.window?.titlebarAppearsTransparent = true
        self.view.window?.titleVisibility = .hidden
        self.title = ""
    }
    
    func setupUI() {
        self.view.addSubview(ktitle)
        ktitle.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(30)
            make.top.equalTo(50)
            make.height.equalTo(40)
        }
        
        self.view.addSubview(mssage)
        mssage.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(ktitle.snp.bottom).offset(10)
            make.width.equalTo(self.view.frame.width-60)
            make.height.equalTo(100)
        }
        
        self.view.addSubview(openButtom)
        openButtom.snp.makeConstraints { (make) in
            make.left.equalTo(30)
            make.bottom.equalTo(-40)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
        self.view.addSubview(closeButtom)
        closeButtom.snp.makeConstraints { (make) in
            make.width.height.equalTo(14)
            make.top.left.equalTo(15)
        }
    }
    
    lazy var ktitle: NSLabel = {
        let lab = NSLabel.label(title: "重要信息", textColor: color_gray(0), font: NSFont.systemFont(ofSize: 24))
        lab.alignment = .left
        lab.sizeToFit()
        return lab
    }()
    
    lazy var mssage: NSLabel = {
        let lab = NSLabel.label(title: "....", textColor: color_gray(120), font: NSFont.systemFont(ofSize: 14))
        lab.alignment = .left
        lab.maximumNumberOfLines = 0
        lab.sizeToFit()
        return lab
    }()
    
    lazy var openButtom: ZHButton = {
        let btn = ZHButton.init()
        btn.zh_title = "打开链接"
        btn.zh_font = NSFont.systemFont(ofSize: 16)
        btn.zh_backgroundColor = color_main108
        btn.hasBorder = true
        btn.zh_cornerRadius = 6
        btn.target = self
        btn.action = #selector(openButtomAction(sender:))
        return btn
    }()
    
    lazy var closeButtom: ZHButton = {
        let btn = ZHButton.init()
        btn.normalImage = NSImage.image(name:"btn_close_normal") ?? NSImage.init()
        btn.hoverImage = NSImage.image(name:"btn_close_hover") ?? NSImage.init()
        btn.highlightImage = NSImage.image(name:"btn_close_pressed") ?? NSImage.init()
        btn.target = self
        btn.action = #selector(closeButtomAction(sender:))
        return btn
    }()
    
    @objc func openButtomAction(sender:ZHButton) {
        if let urlStirng = self.info.url {
            if let url = URL.init(string: urlStirng) {
                NSWorkspace.shared.open(url)
            }
        }
    }
    
    @objc func closeButtomAction(sender:ZHButton) {
        self.view.window?.close()
    }
}
